<?php
class Admin_AppraiseController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pTid = empty($p['t_id'])?'':Tool_Fnc::safe_string($p['t_id']);
        $pKeyword = empty($p['keyword'])?'':Tool_Fnc::safe_string($p['keyword']);
        $pCreated = empty($p['created'])?'':Tool_Fnc::safe_string($p['created']);
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);

        $tDIMO = new DepartmentinfoModel;
        $tWMO = new WorkerModel;
        $tAMO = new AppraiseModel;

        $tWhere = ' 1 and a.is_delete = 1';

        if(!empty($_COOKIE['admin']['roles'])){
            if($_COOKIE['admin']['roles'] != 1 && $_COOKIE['admin']['roles'] != 2){//管理员之外
                $tSql = 'select w.department department from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$_SESSION['admin']['id'];
                $tAData = $tAMO->query($tSql);
                $tWhere = ' 1 and a.is_delete = 1 and w.department = '.$tAData[0]['department'];
            }
        }

        if(!empty($pKeyword)){
            $tWhere .= ' and w.phone like \'%'.$pKeyword.'%\' or w.name like \'%'.$pKeyword.'%\'';
        }
        if(!empty($pCreated)){
            $tWhere .= ' and FROM_UNIXTIME(a.created,\'%Y-%m-%d\') = \''.$pCreated.'\'';
        }
        if(!empty($pDepartment)){
            $tWhere .= ' and w.department = '.$pDepartment;
        }

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pDepartmentID = empty($p['department_id'])?Tool_Fnc::ajaxMsg('系统错误'):Tool_Fnc::safe_string($p['department_id']);
            $tSql = 'select id,name from '.$tWMO->table.' where department = '.$pDepartmentID;
            $tDatas = $tWMO->query($tSql);
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }
    

        $tSql = 'select count(*) c from '.$tAMO->table.' a left join '.$tWMO->table.' w on a.w_id = w.id left join '.$tDIMO->table.' di on w.department = di.num where '.$tWhere;
        $tCount = $tAMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);
        if(!empty($tCnt)){
            $tSql = 'select w.phone phone,a.id id,w.name name,di.introduce department,a.description description,a.status status,a.score score,a.created created,a.user user from '.$tAMO->table.' a left join '.$tWMO->table.' w on a.w_id  = w.id left join '.$tDIMO->table.' di on w.department = di.num where '.$tWhere.' order by a.id desc limit '.$tPage->limit();        
            $tDatas = $tAMO->query($tSql);
        }

        if(!empty($pTid)){//编辑页面显示
            //$tTData = $tTMO->field('*')->where('id = '.$pTid)->fRow();    
            $tSql = 'select a.*,w.department department,w.name name from '.$tAMO->table.' a left join '.$tWMO->table.' w on a.w_id = w.id where a.id = '.$pTid;
            $tTData = $tAMO->query($tSql);
            Tool_Fnc::ajaxMsg('',1,$tTData[0]);
        }

        //所在部门
        $tDDatas = $tDIMO->field('num,introduce')->fList();

        $this->assign('tDatas',$tDatas);
        $this->assign('tDDatas',$tDDatas);
        $this->assign('tShow',$tPage->show());
        $this->assign('pKeyword',$pKeyword);
        $this->assign('pCreated',$pCreated);
        $this->assign('pDepartment',$pDepartment);
    }
    public function addAction(){
        $p = $_REQUEST;
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pSupportworker = empty($p['supportworker'])?Tool_Fnc::ajaxMsg('请先选择员工'):Tool_Fnc::safe_string($p['supportworker']);
            $pDescription = empty($p['description'])?Tool_Fnc::ajaxMsg('考核原因不能为空'):Tool_Fnc::safe_string($p['description']);
            $pStatus = empty($p['status'])?Tool_Fnc::ajaxMsg('加分状态不能为空'):Tool_Fnc::safe_string($p['status']);
            $pScore = empty($p['score'])?Tool_Fnc::ajaxMsg('分数不能为空'):Tool_Fnc::safe_string($p['score']);

            $tTime = time();

            $tData = array(
                'w_id' => $pSupportworker,
                'description' => $pDescription,
                'status' => $pStatus,
                'score' => $pScore,
                'user' => $_SESSION['admin']['name'],
                'is_delete' => 1,
                'created' => $tTime,
            );
            
            $tMO = new AppraiseModel;

            if(!$tId = $tMO->insert($tData)){
                Tool_Fnc::ajaxMsg('添加失败');
            }
            Tool_Fnc::ajaxMsg('添加成功',1);
        }
    
    }
    public function editAction(){
        $p = $_REQUEST;
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pTid = empty($p['tId'])?Tool_Fnc::ajaxMsg('tId不能为空'):Tool_Fnc::safe_string($p['tId']);    
            $pSupportworker = empty($p['supportworker'])?Tool_Fnc::ajaxMsg('请先选择员工'):Tool_Fnc::safe_string($p['supportworker']);
            $pDescription = empty($p['description'])?Tool_Fnc::ajaxMsg('考核原因不能为空'):Tool_Fnc::safe_string($p['description']);
            $pStatus = empty($p['status'])?Tool_Fnc::ajaxMsg('加分状态不能为空'):Tool_Fnc::safe_string($p['status']);
            $pScore = empty($p['score'])?Tool_Fnc::ajaxMsg('分数不能为空'):Tool_Fnc::safe_string($p['score']);

            $tTime = time();

            $tData = array(
                'w_id' => $pSupportworker,
                'description' => $pDescription,
                'status' => $pStatus,
                'score' => $pScore,
                'user' => $_SESSION['admin']['name'],
                'updated' => $tTime,
                'is_delete' => 1,
                'id' => $pTid,
            );
            
            $tMO = new AppraiseModel;

            if(!$tMO->update($tData)){
                Tool_Fnc::ajaxMsg('修改失败');
            }
            Tool_Fnc::ajaxMsg('修改成功',1);
        }
    }
    public function deleteAction(){
        $p = $_REQUEST;
        $pTid = empty($p['id'])?Tool_Fnc::ajaxMsg('id不能为空'):Tool_Fnc::safe_string($p['id']);
        $tTime = time();

        $tAMO = new AppraiseModel;
        $tData = array(
            'is_delete' => 2,
            'id' => $pTid,
            'updated' => $tTime,
        );
        if(!$tAMO->update($tData)){
            Tool_Fnc::ajaxMsg('删除失败');
        }
        Tool_Fnc::ajaxMsg('删除成功',1);
    }
}
?>
